﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace Chenxin.Lib.Passport.DAL
{
    public class User_Permits
    {
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(Guid gdUserId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT DISTINCT P.PermitCode");
            strSql.Append(" FROM Hr_UserInfo U,Base_RoleUserRelationInfo UR,Base_RolePermitionRelationInfo RP,Base_Permit_Define P ");
            strSql.Append(" WHERE U.UserId = UR.UserId AND UR.RoleId = RP.RoleId AND RP.PermitId = P.PermitID AND U.UserId = @gdUserId");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            db.AddInParameter(dbCommand, "gdUserId", DbType.Guid, gdUserId);

            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 根据userid获得用户角色
        /// </summary>
        public static DataSet GetRoleListByUserid(string useridlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT a.userid,b.recordid,c.*");
            strSql.Append(" FROM Hr_UserInfo a left join Base_RoleUserRelationInfo b on a.userid=b.userid inner join Base_Permit_RoleDefine c on b.roleid=c.roleid ");
            strSql.Append(" WHERE a.userid in ("+useridlist+")");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            return db.ExecuteDataSet(dbCommand);
        }
    }
}
